package tt.com.datamanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.ClassesTeam;
import tt.com.common.model.CremateStove;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.service.ClassesTeamService;

@RestController
@RequestMapping(value = "classesteam/manager")
public class ClassesTeamController {

    @Autowired
    private ClassesTeamService classesTeamService;

    @PostMapping(value = "show/page")
    public CommonResult showAllClassesTeamByPage(@RequestBody PageUtil pageUtil){
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = classesTeamService.selectAllClassesTeamByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "所有排班信息查询成功", pageUtil);
    }

    @PostMapping(value = "update")
    public CommonResult updateClassesTeam(@RequestBody ClassesTeam classesTeam){
        if (classesTeam == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (classesTeamService.updateClassesTeam(classesTeam, "1") > 0) {
            return CommonResult.buildR(Status.OK, "排班信息修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "排班信息修改失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult deleteClassesTeam(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = classesTeamService.deleteClassesTeam(id,"1");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "排班信息不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "排班信息已删除");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "排班信息删除成功");
        }
    }

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody ClassesTeam classesTeam) {
        if (!ObjectUtil.objectIsNull(classesTeam)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if(classesTeam.getStartTime()==null || classesTeam.getEndTime()==null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "请设置排班开始时间，结束时间");
        }
        Integer result = classesTeamService.insertClassesTeam(classesTeam);
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "排班添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "排班添加失败");
        }
    }


}
